package com.service.admin.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.service.admin.order.service.OrderProductOtherTotalService;
import com.service.dao.mapper.OrderProductOtherTotalMapper;
import com.service.dao.model.OrderProductOtherTotal;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


@Service("orderProductOtherTotalService")
public class OrderProductOtherTotalServiceImpl extends ServiceImpl<OrderProductOtherTotalMapper, OrderProductOtherTotal> implements OrderProductOtherTotalService {


    @Override
    public List<OrderProductOtherTotal> listTotalByOrderId(Integer orderId) {
        return list(new LambdaQueryWrapper<OrderProductOtherTotal>().eq(OrderProductOtherTotal::getOrderId, orderId));
    }

    @Override
    public Map<String, BigDecimal> listTotal4PayByOrderId2Map(Integer orderId) {

        List<OrderProductOtherTotal> resultList = listTotalByOrderId(orderId);
        Map<String, BigDecimal> resultMap = Maps.newHashMap();
        for (OrderProductOtherTotal item : resultList){
            resultMap.put(item.getTotalPriceCoin(), resultMap.getOrDefault(item.getTotalPriceCoin(), BigDecimal.ZERO).add(item.getTotalPrice()));
            resultMap.put(item.getTotalFreightCoin(), resultMap.getOrDefault(item.getTotalFreightCoin(), BigDecimal.ZERO).add(item.getTotalFreight()));
        }

        return resultMap;
    }

    @Override
    public Map<String, BigDecimal> listTotalByOrderId2Map(Integer orderId) {
        return listTotalByOrderId2Map(listTotalByOrderId(orderId));
    }

    @Override
    public Map<String, BigDecimal> listTotalByOrderId2Map(List<OrderProductOtherTotal> totalList) {
        Map<String, BigDecimal> resultMap = Maps.newHashMap();
        for (OrderProductOtherTotal item : totalList){
            resultMap.put(item.getTotalPriceCoin(), resultMap.getOrDefault(item.getTotalPriceCoin(), BigDecimal.ZERO).add(item.getTotalPrice()));
            resultMap.put(item.getTotalFreightCoin(), resultMap.getOrDefault(item.getTotalFreightCoin(), BigDecimal.ZERO).add(item.getTotalFreight()));
            resultMap.put(item.getTotalGiftCoin(), resultMap.getOrDefault(item.getTotalGiftCoin(), BigDecimal.ZERO).add(item.getTotalGift()));
        }
        return resultMap;
    }
}
